<template>
  <el-dialog :title="form.residentId ? '更改住户' : '增加住户'" width="600" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
      <el-form-item label="姓名" prop="residentName">
        <el-input v-model="form.residentName" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话"/>
      </el-form-item>
      <el-form-item label="性别" prop="residentGender">
        <el-select v-model="form.residentGender" placeholder="请选择性别" clearable>
          <el-option
            v-for="item in genderDict"
            :key="item.id"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" >
        <el-input type="textarea" maxlength="150" rows="3" v-model="form.remark" placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="systemResidentDialog">
import {useMessage} from '/@/hooks/message';
import {addResident,updateResident,getResidentById} from "/@/api/resident/resident";

import { getDicts } from '/@/api/admin/dict';

const genderDict = ref([]);

const loadGenderDict = async () => {
    const res = await getDicts('gender');
    genderDict.value = res.data || [];
};

onMounted(() => {
  loadGenderDict();
});


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 提交表单数据
const form = reactive({
  residentId: '',
  residentGender: '',
  residentName: '',
  phone: '',
  remark: '',
  delFlag: '',
  createTime: '',
  createBy: '',
  updateTime: '',
  updateBy: '',
});

// 定义校验规则
const dataRules = ref({
  residentGender: [
    {required: true, message: '性别不能为空', trigger: 'blur'},
    {
      trigger: 'blur',
    },
  ],
  residentName: [
    {required: true, message: '姓名不能为空', trigger: 'blur'},
    {
      trigger: 'blur',
    },
  ],
  phone: [{required: true, message: '联系电话不能为空', trigger: 'blur'}],
  remark: [{required: true, message: '备注不能为空', trigger: 'blur'}],
});

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true;
  form.residentId = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取Post信息
  if (id) {
    form.residentId = id;
    getResidentData(id);
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.residentId ? await updateResident(form) : await addResident(form);
    useMessage().success((form.residentId ? '修改成功' : '增加成功'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 初始化表格数据
const getResidentData = (id: number) => {
  // 获取部门数据
  getResidentById(id).then((res: any) => {
    Object.assign(form, res.data);
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
